package structs;

import java.util.ArrayList;

import interfaces.ICard;
import interfaces.IPlayer;

public class BooleanStrategy {

	private Game game;
	private ICard [] currentCardsPlayed;
	private Deck deck;
	private int turn;
	private int aiPosition;
	private int teamMatePosition;
	private IPlayer ai;
	private IPlayer teamMate;
	private ArrayList<ICard> currentHand = new ArrayList<ICard>();
	private CardStrategy cardStrategy;

	private ICard hand1;
	private ICard hand2;
	private ICard hand3;


	public BooleanStrategy(Game game, CardStrategy cardStrategy){
		this.game = game;
		this.ai = this.game.getAIPlayer();
		this.teamMate = this.game.getTeamPlayer();
		this.cardStrategy = cardStrategy;
	}
	
	public void updateBooleanStrategy()
	{
		this.deck = this.game.getDeck();
		this.currentCardsPlayed = this.game.getCurrentCardsPlayed();
		this.turn = this.game.getCurrentHandTurn();
		this.aiPosition = this.game.getAIPosition();
		this.teamMatePosition = this.game.getTeamPosition();
		this.currentHand = this.ai.getCurrentHand();
		
		if(this.game.getRoundNumber()<=8)
		{
			this.hand1 = this.currentHand.get(0);
			this.hand2 = this.currentHand.get(1);
			this.hand3 = this.currentHand.get(2);
		}
		
		else if(this.game.getRoundNumber()==9)
		{
			this.hand1 = this.currentHand.get(0);
			this.hand2 = this.currentHand.get(1);
		}
		
		else
		{
			this.hand1 = this.currentHand.get(0);
		}
	}

	//Who is winning this hand
	public boolean areWeWinning(){
		//Test who won and put cards to winners winStack
		ICard max = this.currentCardsPlayed[0];
		int maxPos = 0;
		String playSuit = this.currentCardsPlayed[0].getSuit();


		for(int i = 1 ; i < this.turn; i++){
			if(	
					(!max.getSuit().equalsIgnoreCase(this.deck.getLife().getSuit()) && this.currentCardsPlayed[i].getSuit().equalsIgnoreCase(this.deck.getLife().getSuit())) ||
					(max.getSuit().equalsIgnoreCase(this.deck.getLife().getSuit()) && this.currentCardsPlayed[i].getSuit().equalsIgnoreCase(this.deck.getLife().getSuit()) && max.getPoints() < this.currentCardsPlayed[i].getPoints()) ||
					(this.currentCardsPlayed[i].getSuit().equalsIgnoreCase(playSuit) && max.getPoints() < this.currentCardsPlayed[i].getPoints()) ||
					(this.currentCardsPlayed[i].getSuit().equalsIgnoreCase(playSuit) && this.currentCardsPlayed[i].getPoints() == 0 && max.getPoints() == 0 && this.currentCardsPlayed[i].getNumber() > max.getNumber())
			){
				max = this.currentCardsPlayed[i];
				maxPos = i+1;

			}
		}

		if(maxPos == this.aiPosition || maxPos == this.teamMatePosition)
		{
			return true;
		}

		return false;
	}



	public boolean canWeWin(){
		return false;

	}

	public boolean doWeNeedToWin(){

		if((this.game.getAIPosition()== 2) && ((cardStrategy.currentCardsPlayedPoints() >= 10)
				|| ((cardStrategy.currentCardsPlayedPoints() + this.game.getOpponentsWinStackPoints())>=50)))
		{
			return true;
		}
		
		if((this.game.getAIPosition()== 3) && ((cardStrategy.currentCardsPlayedPoints() > 10)
				|| ((cardStrategy.currentCardsPlayedPoints() + this.game.getOpponentsWinStackPoints())>=50)))
		{
			return true;
		}


		else if((this.game.getAIPosition() ==4) && ((cardStrategy.currentCardsPlayedPoints() > 11)
				|| ((cardStrategy.currentCardsPlayedPoints() + this.game.getOpponentsWinStackPoints())>=50)))
		{
			return true;
		}

		return false;	
	}
}
